package com.aramco.fion.util
{
	import com.aramco.fion.vo.ComponentVO;
	import com.aramco.fion.vo.DashboardVo;
	import com.aramco.fion.vo.FionVo;
	import com.aramco.fion.vo.GroupVO;
	import com.aramco.fion.vo.GroupViewVO;
	import com.aramco.fion.vo.SummaryViewVO;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Tree;
	import mx.core.ComponentDescriptor;
	import mx.logging.ILogger;
	import mx.rpc.events.ResultEvent;
	
	import org.esria.samples.dashboard.view.ab;
	
	public class XmlTreeUtil
	{
		public static function parseAppXML(xml:XML):FionVo
		{
			var fion:FionVo = new FionVo();
			fion.id = xml.@id;
			fion.title = xml.@title;
			
			var dashboardsList:Array = new Array();	
			for each ( var element:XML in xml..dashboard) {
				var db:DashboardVo = new DashboardVo();
				db.id = element.@id.toString();
				db.title = element.@title.toString();
				db.config = element.config;
				db.views = new Array();
				
				if(element.views.children().length() > 0)
				{
					var views:Array = new Array();
					for each ( var element2:XML in element.views.children()) {
						var viewType:String = element2.name().toString();
						//Summary View
						if(viewType == "summaryView")
						{
							var summaryVo:SummaryViewVO = new SummaryViewVO();
							// to be implemented
							//summaryVo.components = element2.components;
							//summaryVo.selections = element2.selections;
							summaryVo.id = element2.@id;
							summaryVo.title = element2.@title;
							summaryVo.layout = element2.@layout;
							summaryVo.components = new Array();
							
							//Getting Components under Summary View
							for each(var element02:XML in element2..component)
							{
								var compVo:ComponentVO = new ComponentVO();
								compVo.id = element02.@id;
								compVo.queryName = element02.@queryName;
								compVo.title = element02.@title;
								compVo.type = element02.@type;
								compVo.url = element02.@url;
								compVo.config = element02.config;
								compVo.selections = new ArrayCollection();
								summaryVo.components.push(compVo);									
							}
							
							views.push(summaryVo);
						}
						
						//Group View
						else if(viewType == "groupView")
						{
							var groupViewVo:GroupViewVO = new GroupViewVO();
							groupViewVo.id = element2.@id;
							groupViewVo.title = element2.@title;
							groupViewVo.config = element2.config;
							
							//Getting Groups
							if(element2..group.length() > 0)
							{
								var groups:Array = new Array();
								var groupVo:GroupVO;
								groupViewVo.groups = new ArrayCollection();
								
								//Getting the groups under GroupView
								for each ( var element4:XML in element2..group) {
									groupVo = new GroupVO();
									groupVo.id = element4.@id;
									groupVo.title = element4.@title;
									groupVo.components = new Array();
									
									//Getting Components under group
									for each(var element5:XML in element4..component)
									{
										var compVo:ComponentVO = new ComponentVO();
										compVo.id = element5.@id;
										compVo.queryName = element5.@queryName;
										compVo.title = element5.@title;
										compVo.type = element5.@type;
										compVo.url = element5.@url;
										compVo.config = element5.config;
										compVo.selections = new ArrayCollection();
										groupVo.components.push(compVo);									
									}
									groupViewVo.groups.addItem(groupVo);
								}
							}
							views.push(groupViewVo);
						}
						db.views = views;
					}
				}
				dashboardsList.push(db);
			}
			fion.dashboards = dashboardsList;
			return fion;
		}
		
		public static function getNavigationXMLByComponents(value:FionVo):XML
		{
			var xm:XML = <root></root>;
			for(var i:int = 0 ; i < value.dashboards.length;i++)
			{
				var dashboardList:XMLList = XMLList('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).id+'"'+ ' title="' +(value.dashboards[i] as DashboardVo).title+ '"></node>');
				trace((value.dashboards[i] as DashboardVo).title);
				for(var x:int = 0; x < (value.dashboards[i] as DashboardVo).views.length; x++)
				{
					var viewList:XMLList = new XMLList('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).views[x]['id'] +'"'+ 
						' title="' + (value.dashboards[i] as DashboardVo).views[x]['title'] +'"'+ '></node>');
					
					dashboardList.appendChild(viewList);
					
					
					trace('-'+(value.dashboards[i] as DashboardVo).views[x]['title']);
					if((value.dashboards[i] as DashboardVo).views[x] is SummaryViewVO)
					{
						//var compList:XMLList = 
						for(var y:int = 0 ; y < (value.dashboards[i] as DashboardVo).views[x].components.length; y++)
						{
							trace('--'+(value.dashboards[i] as DashboardVo).views[x].components[y]['title']);
							viewList.appendChild((new XMLList('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).views[x].components[y]['id'] +'"'+ 
								' title="' + (value.dashboards[i] as DashboardVo).views[x].components[y]['title'] +'"'+ '></node>')));
						}
					}
						
					else if((value.dashboards[i] as DashboardVo).views[x] is GroupViewVO)
					{
						//
						for(var m:int = 0 ; m < (value.dashboards[i] as DashboardVo).views[x].groups.length; m++)
						{
							var groupList:XMLList = XMLList((('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).views[x].groups[m]['id'] +'"'+ 
								' title="' + (value.dashboards[i] as DashboardVo).views[x].groups[m]['title'] +'"'+ '></node>')));
							for(var n:int = 0; n < (value.dashboards[i] as DashboardVo).views[x].groups[m].components.length; n++)
							{
								trace('--'+(value.dashboards[i] as DashboardVo).views[x].groups[m].components[n]['title']);
								groupList.appendChild((new XMLList('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).views[x].groups[m].components[n]['id'] +'"'+ 
									' title="' + (value.dashboards[i] as DashboardVo).views[x].groups[m].components[n]['title'] +'"'+ '></node>')));
							}
						}
						viewList.appendChild(groupList);
					}
					
				}
				xm.appendChild(dashboardList);
			}
			return xm;
		}
		
	}
}
